﻿using System;
using System.Threading;
using System.Windows.Forms;
using Aspects.Custom.Audio;
using Aspects.Unit;

namespace Aspects.Panels.Main_Panels
{
    public partial class AudioBrightnessPanel : UserControl
    {
        private WaveOut waveOut;
        private WaveIn waveIn;
        private SineWaveProvider sineWaveProvider;
        private System.Windows.Forms.Timer SoundTimer;
        public AudioBrightnessPanel()
        {
            InitializeComponent();
            SoundTimer = new System.Windows.Forms.Timer();
            SoundTimer.Tick += new EventHandler(SoundTimer_Tick);
        }



        public void UpdateBrightnessTrackbar()
        {
            trackBar1.Value = UnitDisplay.GetBrightness();
        }

        private void speakerTestButton_Click(object sender, EventArgs e)
        {
            sineWaveProvider = new SineWaveProvider();

            sineWaveProvider.Amplitude = toneAmplitudeTrackBar.Value/100.0f;
            waveOut = new WaveOut(sineWaveProvider);
            StartSound();
            PlayTone(250,5000, 250);
            PlayTone(5000,250, -250);
            StopSound();
        }

        private void microphoneTestButton_Click(object sender, EventArgs e)
        {
            microphoneSoundbar.Amplitude = 0;
            microphoneSoundbar.MaxAmplitude = 0;
            try
            {
                waveIn = new WaveIn();
                waveIn.MaxSoundLevel += new WaveIn.MaxSoundHandler(waveIn_MaxSoundLevel);
                waveIn.StartRecording();
                StartSound();
                SoundTimer.Interval=5000;
                SoundTimer.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Fial");
            }
        }

        private void brightnessMoreButton_Click(object sender, EventArgs e)
        {
            UnitDisplay.Brighten();
            UpdateBrightnessTrackbar();
        }

        private void brightnessLessButton_Click(object sender, EventArgs e)
        {
            UnitDisplay.Darken();
            UpdateBrightnessTrackbar();
        }

        private void waveIn_MaxSoundLevel(int MaxSound)
        {
            microphoneSoundbar.Amplitude = MaxSound;
        }
        
        private void SoundTimer_Tick(object sender, EventArgs e)
        {
            SoundTimer.Stop();
            StopSound();
        }

        private void StartSound()
        {
            speakerTestButton.Enabled = false;
            microphoneTestButton.Enabled = false;
            toneAmplitudeTrackBar.Enabled = false;
        }

        public void StopSound()
        {
            try
            {
                speakerTestButton.Enabled = true;
                microphoneTestButton.Enabled = true;
                toneAmplitudeTrackBar.Enabled = true;                
                if (waveOut != null)
                {
                    waveOut.Stop();
                    waveOut = null;
                }
                if (waveIn != null)
                {
                    waveIn.StopRecording();
                    waveIn = null;
                    microphoneSoundbar.Amplitude = microphoneSoundbar.MaxAmplitude;
                }
            }
            catch
            {
                //Do nothing
            }
        }
        
        private void PlayTone(int startFrequency, int endFrequency, int interval)
        {
            for (int i = startFrequency; i != endFrequency; i+=interval)
            {
                sineWaveProvider.Frequency = i;
                waveOut.Play();
                Thread.Sleep(75);
                waveOut.Stop();
            }
        }
    }
}
